package com.nip.workmanager.database;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.nip.workmanager.common.ConstantDatabase;
import com.nip.workmanager.common.utilities.UtilitiesDataBase;
import com.nip.workmanager.common.utilities.UtilitiesObjectId;
import com.nip.workmanager.entity.TodoEntity;

public class TodoTable extends SQLiteOpenHelper {

	private Context _mContext;;

	public TodoTable(Context context) {
		super(context, ConstantDatabase.DATABASE_NAME, null,
				ConstantDatabase.DATABASE_VERSION);
		// TODO Auto-generated constructor stub
		// SQLiteDatabase.openOrCreateDatabase(Constant.DATABASE_NAME, null);
		_mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
	}

	/**
	 * Get biggest id of todotable
	 * 
	 * @return biggest id
	 */
	private String GetBiggestId() {
		String idResponse = null;
		SQLiteDatabase rdb = getReadableDatabase();

		String strQuery = "SELECT * FROM " + ConstantDatabase.TODO_TABLE_NAME
				+ " ORDER BY (" + ConstantDatabase.TODO_ID + " ) DESC";
		Cursor data = rdb.rawQuery(strQuery, null);
		if (data.moveToFirst()) {
			idResponse = data.getString(data
					.getColumnIndex(ConstantDatabase.TODO_ID));
		}

		return idResponse;
	}

	/**
	 * Get all todo by work id
	 * 
	 * @param workId
	 * @return
	 */
	public List<TodoEntity> GetToDoByWorkId(String workId) {

		List<TodoEntity> listResponse = null;
		SQLiteDatabase rdb = getReadableDatabase();
		try {
			Cursor data = rdb.query(ConstantDatabase.TODO_TABLE_NAME,
					new String[] { ConstantDatabase.TODO_ID,
							ConstantDatabase.TODO_NAME,
							ConstantDatabase.TODO_STATUS,
							ConstantDatabase.TIME,
							ConstantDatabase.DESCRIPTION,
							ConstantDatabase.WORK_ID },
					ConstantDatabase.WORK_ID + "=?", new String[] { workId },
					null, null, null);
			if (data.moveToFirst()) {
				TodoEntity mTodo;
				listResponse = new ArrayList<TodoEntity>();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						ConstantDatabase.DATE_TIME_FORMAT);
				Boolean isOn;
				do {
					mTodo = new TodoEntity();
					mTodo.setToDoId(data.getString(data
							.getColumnIndex(ConstantDatabase.TODO_ID)));
					mTodo.setToDoName(data.getString(data
							.getColumnIndex(ConstantDatabase.TODO_NAME)));
					mTodo.setDescription(data.getString(data
							.getColumnIndex(ConstantDatabase.DESCRIPTION)));
					isOn = data.getString(
							data.getColumnIndex(ConstantDatabase.TODO_STATUS))
							.equals("1");
					mTodo.setToDoStatus(isOn);
					mTodo.setTime(data.getString(data
							.getColumnIndex(ConstantDatabase.TIME)));
					mTodo.setWorkId(data.getString(data
							.getColumnIndex(ConstantDatabase.WORK_ID)));
					listResponse.add(mTodo);
				} while (data.moveToNext());

			}
		} catch (Exception e) {
			// TODO: handle exception
			listResponse = null;
			Log.e("GetToDoByWorkId", e.getMessage());
		} finally {
			rdb.close();
		}
		return listResponse;
	}

	/**
	 * Get all todo in table
	 * 
	 * @return ArryList<TodoEntity>
	 */
	public ArrayList<TodoEntity> GetAllTodo() {
		SQLiteDatabase rdb = getReadableDatabase();
		ArrayList<TodoEntity> listSource = null;
		TodoEntity todo;
		String query = "SELECT * FROM " + ConstantDatabase.TODO_TABLE_NAME;
		try {
			Cursor data = rdb.rawQuery(query, null);
			if (data.moveToFirst()) {
				listSource = new ArrayList<TodoEntity>();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						ConstantDatabase.DATE_TIME_FORMAT);
				Boolean isOn;
				do {
					todo = new TodoEntity();

					todo.setToDoId(data.getString(data
							.getColumnIndex(ConstantDatabase.TODO_ID)));
					todo.setToDoName(data.getString(data
							.getColumnIndex(ConstantDatabase.TODO_NAME)));
					todo.setWorkId(data.getString(data
							.getColumnIndex(ConstantDatabase.WORK_ID)));
					todo.setDescription(data.getString(data
							.getColumnIndex(ConstantDatabase.DESCRIPTION)));
					todo.setTime(data.getString(data
							.getColumnIndex(ConstantDatabase.TIME)));
					isOn = data.getString(
							data.getColumnIndex(ConstantDatabase.TODO_STATUS))
							.equals("1");
					todo.setToDoStatus(isOn);
					listSource.add(todo);
				} while (data.moveToNext());
			}
		} catch (Exception e) {
			// TODO: handle exception
			listSource = null;
			Log.e("GetAllTodo", e.getMessage());
		} finally {
			rdb.close();
		}
		return listSource;
	}

	/**
	 * Insert todo in table name
	 * 
	 * @param todo
	 * @return True/False Pass/False
	 */
	public Boolean InsertTodo(TodoEntity todo) {
		SQLiteDatabase wdb = getWritableDatabase();
		Boolean result = false;

		try {
			String id = UtilitiesObjectId.MakeObjecId(GetBiggestId(), "ID", 20);
			ContentValues value = new ContentValues();
			value.put(ConstantDatabase.TODO_ID, id);
			value.put(ConstantDatabase.TODO_NAME, todo.getToDoName());
			value.put(ConstantDatabase.TODO_STATUS, todo.getToDoStatus());
			value.put(ConstantDatabase.DESCRIPTION, todo.getDescription());
			value.put(ConstantDatabase.TIME, todo.getTime());
			value.put(ConstantDatabase.WORK_ID, todo.getWorkId());
			wdb.insert(ConstantDatabase.TODO_TABLE_NAME, null, value);
			result = true;
		} catch (Exception e) {
			// TODO: handle exception
			result = false;
			Log.e("InsertTodo", e.getMessage());
		} finally {
			wdb.close();
		}
		return result;
	}

	/**
	 * 
	 * Update todo in TodoTable by TodoId
	 * 
	 * @param todo
	 *            data update
	 * @return True/False Pass/Fail
	 */
	public Boolean UpdateTodo(TodoEntity todo) {

		SQLiteDatabase wdb = getWritableDatabase();
		Boolean result = false;
		try {
			ContentValues value = new ContentValues();
			value.put(ConstantDatabase.TODO_NAME, todo.getToDoName());
			value.put(ConstantDatabase.TODO_STATUS, todo.getToDoStatus());
			value.put(ConstantDatabase.DESCRIPTION, todo.getDescription());
			value.put(ConstantDatabase.TIME, todo.getTime());

			wdb.update(ConstantDatabase.TODO_TABLE_NAME, value,
					ConstantDatabase.TODO_ID + "=?",
					new String[] { todo.getToDoId() });
			result = true;
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("UpdateTodo", e.getMessage());
			result = false;
		} finally {
			wdb.close();
		}
		return result;
	}

	/**
	 * Delete a todo by id
	 * 
	 * @param id
	 *            Id of todo delete
	 * @return True/False Pass/False
	 */
	public Boolean DeleteTodoById(String id) {
		SQLiteDatabase wdb = getWritableDatabase();
		Boolean result = false;
		try {
			wdb.delete(ConstantDatabase.TODO_TABLE_NAME,
					ConstantDatabase.TODO_ID + "=?", new String[] { id });
			result = true;
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("DeleteTodoById", e.getMessage());
			result = false;
		} finally {
			wdb.close();
		}

		return result;
	}
}
